﻿Public Class CategoriaUnidades 
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub
    
    Private Sub UnidadescategoriaBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles UnidadescategoriaBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.UnidadescategoriaBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo almacenar los datos")
        End Try
     

    End Sub

    Private Sub CategoriaUnidades_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadescategoria' table. You can move, or remove it, as needed.
        Try
            Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Categoria Unidades")
        End Try
        Me.DescriptionTextBox.ReadOnly = True
        Me.ObservacionTextBox.ReadOnly = True
        Me.PanelArriba.Enabled = False
    End Sub

    Private Sub agregar_Click(sender As System.Object, e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
        Me.DescriptionTextBox.ReadOnly = False
        Me.ObservacionTextBox.ReadOnly = False
    End Sub

    Private Sub modificar_Click(sender As System.Object, e As System.EventArgs) Handles modificar.Click
        Mostrar()
        Me.DescriptionTextBox.ReadOnly = False
        Me.ObservacionTextBox.ReadOnly = False
    End Sub

    Private Sub eliminar_Click(sender As System.Object, e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer

            opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                UnidadescategoriaTableAdapter.DeleteQuery(Val(IdunidadescategoriaTextBox.Text))
                AlertControl1.Show(Owner, "Unidades de Medidas", "Se eliminaron los datos")
                Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
            Catch ex As Exception

                AlertControl1.Show(Owner, "ERROR", "Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas")

            End Try
        End If

        'MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
        
    End Sub

    Private Sub guardar_Click(sender As System.Object, e As System.EventArgs) Handles guardar.Click
        If DescriptionTextBox.Text.Length = 0 Then
            ErrorProvider1.SetError(DescriptionTextBox, "Debe ingresar una descripcion de unidad")
        Else
            ErrorProvider1.SetError(DescriptionTextBox, Nothing)
            UnidadescategoriaBindingNavigatorSaveItem.PerformClick()
            AlertControl1.AppearanceCaption.ForeColor = Color.Green
            AlertControl1.Show(Owner, "Unidad Medida", "Se Almaceno correctamente la Unidad")
            Mostrar()
        End If
    End Sub

    Private Sub cancelar_Click(sender As System.Object, e As System.EventArgs) Handles cancelar.Click
        ErrorProvider1.SetError(DescriptionTextBox, Nothing)
        Mostrar()
        Try
            Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Unidades de Medida")
        End Try

    End Sub

    Private Sub imprimir_Click(sender As System.Object, e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class